package com.yld.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.yld.dto.DBDTO;

public class SingleConnection {
	
	
	
	private static  Connection connection=null;
	
	
	
	public static Connection  getConnection(){
		if(null == connection){
			//创建连接
			connection = getConn();
		}
		return connection;
	}
	
	private  SingleConnection(){
		
	}
	
	
	/**
	 * 获取数据库连接
	 * @return
	 * @throws Exception
	 */
	private static Connection getConn(){
		DBDTO dto = PropertiesUtil.parseJdbcFile();
		
		//2 连接数据库
		Connection connection=null;
		try {
			//1 加载驱动程序  连接数据库...
			Class.forName(dto.getDriver());
			connection = DriverManager.getConnection(dto.getUrl(), dto.getUser(), dto.getPassword());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return connection;
	}

}
